// pages/address/user-address/user-address.js
var app = getApp()
var that
Page({
  data: {
    address: [],
    radioindex: '',
    pro_id: 0,
    num: 0,

    froms:"",  //从哪里跳入此界面 
  },
  onLoad: function (options) { 
   that = this;
   if (options.from){ that.setData({ froms: options.from}) }
   if (options.from =='order'){ that.setData({
     goodsid: options.goodsid, optionid: options.optionid, num: options.num, cartids: options.cartids})
    }
  },

  onShow: function () {
    that.set_mo_ren_di_zhi(); //当只有一个时设置默认地址，里面包含重新获取地址
  },
  
  //设置默认地址
  setDefault: function (e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    var isdefault = 0;
    if (that.data.address[e.currentTarget.dataset.idx].isdefault==0)
    {
      that.data.address[e.currentTarget.dataset.idx].isdefault =1;
      isdefault=1
    } else { that.data.address[e.currentTarget.dataset.idx].isdefault = 0}
    // that.setData({ address: that.data.address})
    if (that.data.froms == "order") {
        var pages = getCurrentPages()
        var prevPage = pages[pages.length - 2]  //上一个页面
        var that = this
        prevPage.setData({ addr_id: addrId })
     
        wx.navigateBack({  delta: 1 })
        return;
      }
    app.req("setDefault_shop_adr.html", { "openid": app.data.openid, "id": addrId, "isdefault": isdefault }, function (res) {
      if (res.status == "success") {
        that.set_mo_ren_di_zhi(); //当只有一个时设置默认地址，里面包含重新获取地址
      } else if (res.status == "error") {
        app.toast(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.setDefault(e)
          }
        })
      }
    })
  },
  //编辑地址
  edit:function(e){
    var addrId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../shop_adr/shop_adr_detail?id=' + addrId,
    })
  },
  delAddress: function (e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '你确认移除吗',
      success: function (r) {
        if (r.confirm){ 
          app.req("delAddress.html", { "id": addrId }, function (res) {
            if (res.status == "success") {
              that.set_mo_ren_di_zhi()//判断是否默认，里面包含重新获取地址
            } else if (res.status == "error") {
              app.toast(res.mess)
            } else {
              app.modal("错误提示", "网络错误，是否重试？", function (re) {
                if (re) {
                  that.delAddress(e)
                }
              })
            }
          })
        }
      }
    });
  },
  
  get_adr:function(){
    app.req("get_shop_adr.html", { "openid": app.data.openid, }, function (res) {
      if (res.status == "success") {
        that.setData({ address: res.mess })
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_adr()
          }
        })
      }
    })
  },
  set_mo_ren_di_zhi:function(){
    app.req("set_mo_ren_di_zhi.html", { "openid": app.data.openid, }, function (res) {
      // console.log(res)
      if (res.status == "success") {
        that.get_adr()
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.set_mo_ren_di_zhi()
          }
        })
      }
    })
  }
})